package com.example.exoplayerdemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.exoplayerdemo.databinding.ActivitySimple1Binding
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.util.Util


/**
 *  简单用例
 *  作者：Caowj
 *  邮箱：caoweijian@kedacom.com
 *  日期：2021/8/27 15:31
 */
class SampleActivity1 : AppCompatActivity() {
    private val viewBinding by lazy(LazyThreadSafetyMode.NONE) {
        ActivitySimple1Binding.inflate(layoutInflater)
    }

    private var player: SimpleExoPlayer? = null

    private var playWhenReady = true
    private var currentWindow = 0
    private var playbackPosition = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
    }

    public override fun onStart() {
        super.onStart()
        if (Util.SDK_INT > 23) {
            initializePlayer()
        }
    }

    public override fun onStop() {
        super.onStop()
        if (Util.SDK_INT > 23) {
            releasePlayer()
        }
    }

    private fun initializePlayer() {
        // 1、创建播放器
        player = SimpleExoPlayer.Builder(this)
            .build()
            .also { exoPlayer ->
                // 2、将播放器附加到视图
                viewBinding.playView.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT//设置视频宽高
                viewBinding.playView.player = exoPlayer

                // 3、准备播放资源
//                val mediaItem = MediaItem.fromUri(getString(R.string.media_url_mp4))
                //https证书异常的视频 （java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.）
                val mediaItem = MediaItem.fromUri("https://192.168.20.180/pacc/attachment/v1/paccFileDownload?filePath=2021-08-26/210826145655local_video.mp4&optType=online")
                // MediaPlayer: error (1, -1004)，视频地址支持下载，不支持在线播放
//                val mediaItem =
//                    MediaItem.fromUri("http://192.168.20.180/pacc/attachment/v1/paccFileDownload?filePath=2021-08-16/210816092644210810094303VID_20210810_093643.mp4")
                exoPlayer.setMediaItem(mediaItem)

//                val secondMediaItem = MediaItem.fromUri(getString(R.string.media_url_mp3))
//                exoPlayer.addMediaItem(secondMediaItem)


//                // 使用DashMediaSource
//                val mediaSource: MediaSource = DashMediaSource.Factory(DefaultHttpDataSource.Factory())
//                    .createMediaSource(MediaItem.fromUri(dashUri))
//                exoPlayer.setMediaSource(mediaSource)


                exoPlayer.playWhenReady = playWhenReady//加载好了自动播放
                exoPlayer.seekTo(currentWindow, playbackPosition)
                exoPlayer.prepare()
            }
    }

    private fun releasePlayer() {
        // 4、释放播放器
        player?.run {
            playbackPosition = this.currentPosition
            currentWindow = this.currentWindowIndex
            playWhenReady = this.playWhenReady
            release()
        }
        player = null
    }
}